package com.hxkj.client.service.taglib.directive;

import com.hxkj.client.service.LeafletServiceClient;
import com.hxkj.domain.context.EnvironmentContextHolder;
import com.hxkj.domain.po.Leaflet;
import com.hxkj.domain.po.Tags;
import com.hxkj.domain.po.WebsiteAddr;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

import static com.hxkj.client.service.taglib.config.LableConstant.CMS_LEAFLETDETAIL;

/**
 * 单页详情 自定义标签
 *
 * @author chenfangzhou 2018/09/14
 */
@Component
@Slf4j
public class LeafletDetailDirective extends AbstractDirective {
    @Autowired
    private LeafletServiceClient leafletServiceClient;
    /**
     * 站点ID。
     */
    private static final String SITE_ID = "siteId";
    /**
     * 单页id。
     */
    public static final String LEAFLET_ID = "leafletId";
    /**
     * 单页标题。
     */
    public static final String HEADLINE = "headline";


    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
        try {
            Long siteId = getLong(SITE_ID, params) == null ? (Long) getCustomAttrbute(env, SITE_ID) : getLong(SITE_ID, params);
            Long leafletId = getLong(LEAFLET_ID, params) == null ? getArticleId(env) : getLong(LEAFLET_ID, params);
            String headline = getString(HEADLINE, params);
            String note = super.getNote(env);
            Leaflet leaflet_;
            if (leafletId == null) {
                leaflet_ = new Leaflet();
                log.debug("单页id不能为空!");
            } else {
                Leaflet leaflet = new Leaflet();
                leaflet.setSiteId(siteId);
                leaflet.setLeafletId(leafletId);
                leaflet.setHeadline(StringUtils.isBlank(headline) ? "" : headline);

                //单页详情
                leaflet_ = leafletServiceClient.getLeafletByStatus(leaflet).getData();
                WebsiteAddr webSiteAddr = getWebSiteAddr(siteId);

                if (null != leaflet_) {
                    if (StringUtils.isNotBlank(note)) {
                        leaflet_.setManuscriptPath(webSiteAddr.getIpAdd() + leaflet_.getManuscriptPath());
                    }else{
                        Map<String, Object> enviroment = EnvironmentContextHolder.getEnvironment();
                        Map<Long, String> siteCtxMap = (Map<Long, String>) enviroment.get("siteCtxMap");
                        String urlContext = siteCtxMap.get(siteId);
                        if(StringUtils.isNotBlank(urlContext)) {
                            leaflet_.setManuscriptPath(urlContext + leaflet_.getManuscriptPath());
                        }
                    }
                } else {
                    leaflet_ = new Leaflet();
                    log.debug("没有获取到单页数据!");
                }
            }

            env.setVariable("leaflet", DefaultObjectWrapperBuilderFactory.getDefaultObjectWrapper().wrap(leaflet_));

            String userToken = super.getUserToken(env);
            if (body != null) {
                if (StringUtils.isEmpty(note)) {
                    body.render(env.getOut());
                } else {
                    Map<String, String> maps = new LinkedHashMap<>();

                    maps.put(CMS_LEAFLETDETAIL, "");
                    maps.put(SITE_ID, String.valueOf(getLong(SITE_ID, params)));
                    maps.put(LEAFLET_ID, String.valueOf(getLong(LEAFLET_ID, params)));
                    try {
                        super.print(body, maps, env, userToken, params);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } else {
                log.debug("单页详情所在模板至少要加一个空格");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
